package com.hk.util;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component;

import java.util.Arrays;


@Component("util")
@Aspect("pt")
public class LogUtil {
    @Pointcut(value = "execution(* com.hk.impl.RentImpl.*(..))")
    public static void pc(){}

//    @Before(value = "pc()")
    public static void start(JoinPoint joinPoint){

//        获取方法名
        Signature signature = joinPoint.getSignature();
        String name = signature.getName();
        Object[] args = joinPoint.getArgs();
        System.out.println(name+"方法开始执行,参数是"+ Arrays.asList(args));
    }
//    @AfterReturning(value = "pc()",returning = "result")
    public static void returnResult(JoinPoint joinPoint,Object result){
        String name = joinPoint.getSignature().getName();
        System.out.println(name+"方法直接结束，返回结果是"+result);
    }
//    @AfterThrowing(value = "pc()",throwing = "e")
    public static void throwEx(JoinPoint joinPoint,Exception e)
    {
        String name = joinPoint.getSignature().getName();
        System.out.println(name+"方法执行出现异常==="+e.getMessage());
    }
//    @After(value = "pc()")
    public static void end(JoinPoint joinPoint)
    {
        String name = joinPoint.getSignature().getName();
        System.out.println(name+"方法最终执行结束===");
    }
//    @Around(value = "pc()")
    public Object around(ProceedingJoinPoint pjp){
        Object proceed=null;
        try {
            System.out.println("环绕通知的前置");
            proceed= pjp.proceed(pjp.getArgs());
            System.out.println("环绕通知的返回结果是"+proceed);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
            System.out.println("环绕通知的异常时"+throwable.getMessage());
        }finally {
            System.out.println("环绕通知的最终结束");
        }
        return proceed;
    }
}
